import os

from common.entitys.monitor import MonitorType
from utils import statisticalutil, parse_util, excel_util, read_config, date_util
from database.mapper import stock_day_k, stock_info, monitor_result
from database.service import monitor_result_service


# 中国铝业
# 1.合并消息-利好s
# 2.k线形态
# 3.起始形态
# 4 2021-11-18 开始操作
def example1():
    k_list = stock_day_k.select_interval_asc('601600', '2021-09-17', '2021-11-18')
    return {'ks': k_list, 'sz': (400, 2500), 'size': len(k_list)}


# 筛选出所有实例相似
def find():
    s_all_dto = stock_info.select_all()

    # 1 实例1
    result_e1 = example1()
    x = parse_util.list_dict_to_list(result_e1.get('ks'), 'sp')
    result_list = []
    for s_dto in s_all_dto:
        if s_dto.total_val < result_e1.get('sz')[0] or s_dto.total_val > result_e1.get('sz')[1]:
            continue
        ks = stock_day_k.select_size(s_dto.code, result_e1.get('size'))
        ks.reverse()
        y = parse_util.list_dict_to_list(ks, 'sp')
        if statisticalutil.correlation(x, y) > 0.8:
            info = s_dto.__dict__
            info['score'] = statisticalutil.correlation(x, y)
            info['example'] = 1
            result_list.append(info)
    if read_config.sys == 'win':
        excel_util.gen_an_excel(os.path.join(read_config.data_path, 'result', '实例筛选', '验证实例1'),
                                date_util.get_date_str(), '', parse_util.list_dict_to_arr(result_list))
    else:
        monitor_result_service.batch_insert_list_dict(result_list, MonitorType.five, '验证实例1')


if __name__ == '__main__':
    print('')
    find()
